

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.net.async.msg &mdash; cup 1.7.0 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../../genindex/" />
    <link rel="search" title="Search" href="../../../../../search/" /> 

  
  <script src="../../../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../../../" class="icon icon-home"> cup
          

          
          </a>

          
            
            
              <div class="version">
                1.7
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../../search/" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../cup/">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../../">cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../../">Docs</a> &raquo;</li>
        
          <li><a href="../../../../">Module code</a> &raquo;</li>
        
          <li><a href="../../">cup.net</a> &raquo;</li>
        
      <li>cup.net.async.msg</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.net.async.msg</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma (@mythmgn)</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    netmsg related module</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="kn">import</span> <span class="nn">cup</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">log</span>
<span class="kn">from</span> <span class="nn">cup.util</span> <span class="k">import</span> <span class="n">misc</span>
<span class="kn">from</span> <span class="nn">cup.util</span> <span class="k">import</span> <span class="n">generator</span>
<span class="kn">from</span> <span class="nn">cup.net.async</span> <span class="k">import</span> <span class="n">common</span>


<span class="n">MSG_RESENDING_FLAG</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">MSG_RESEND_SUCCESS</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">MSG_TIMEOUT_TO_DELETE</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">MSG_DELETE_FLAG</span> <span class="o">=</span> <span class="mi">3</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;CMsgType&#39;</span><span class="p">,</span> <span class="s1">&#39;CMsgFlag&#39;</span><span class="p">,</span> <span class="s1">&#39;CNetMsg&#39;</span><span class="p">,</span> <span class="s1">&#39;CAckMsg&#39;</span><span class="p">,</span> <span class="s1">&#39;netmsg_tostring&#39;</span><span class="p">]</span>


<span class="n">MSG_TYPE2NUM</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;HEART_BEAT&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
    <span class="s1">&#39;RESOURCE_ACQUIRE&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
    <span class="s1">&#39;RESOURCE_RELEASE&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
    <span class="s1">&#39;ACK_OK&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
    <span class="s1">&#39;ACK_FAILURE&#39;</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span>
    <span class="s1">&#39;ACK_HEART_BEAT&#39;</span><span class="p">:</span> <span class="mi">6</span><span class="p">,</span>
    <span class="s1">&#39;ACK_CREATE&#39;</span><span class="p">:</span> <span class="mi">7</span><span class="p">,</span>
    <span class="s1">&#39;NEED_ACK&#39;</span><span class="p">:</span> <span class="mi">8</span>
<span class="p">}</span>


<span class="n">MSG_FLAG2NUM</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;FLAG_URGENT&#39;</span><span class="p">:</span> <span class="mh">0X00000001</span><span class="p">,</span>
    <span class="s1">&#39;FLAG_NORMAL&#39;</span><span class="p">:</span> <span class="mh">0X00000002</span><span class="p">,</span>
    <span class="s1">&#39;FLAG_NEEDACK&#39;</span><span class="p">:</span><span class="mh">0X00000004</span><span class="p">,</span>
    <span class="s1">&#39;FLAG_ACK&#39;</span><span class="p">:</span> <span class="mh">0X00000008</span><span class="p">,</span>
<span class="p">}</span>


<span class="nd">@cup</span><span class="o">.</span><span class="n">decorators</span><span class="o">.</span><span class="n">Singleton</span>
<span class="k">class</span> <span class="nc">CMsgType</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    for netmsg types</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_type2number</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_number2type</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">register_types</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">kvs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        register types</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">key_value</span> <span class="ow">in</span> <span class="n">kvs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_type2number</span><span class="p">[</span><span class="n">key_value</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">key_value</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_number2type</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">key_value</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span> <span class="o">=</span> <span class="n">key_value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">gettype_bynumber</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">number</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get type by number</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_number2type</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">number</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">getnumber_bytype</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">str_type</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get number by type</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_type2number</span><span class="p">[</span><span class="n">str_type</span><span class="p">]</span>


<span class="nd">@cup</span><span class="o">.</span><span class="n">decorators</span><span class="o">.</span><span class="n">Singleton</span>
<span class="k">class</span> <span class="nc">CMsgFlag</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    msg flag class</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_flag2number</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_number2flag</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">register_flags</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">kvs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        register flags</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">key_value</span> <span class="ow">in</span> <span class="n">kvs</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_flag2number</span><span class="p">[</span><span class="n">key_value</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">key_value</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_number2flag</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">key_value</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span> <span class="o">=</span> <span class="n">key_value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">getflag_bynumber</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">number</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get flag by number</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_number2flag</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">number</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">getnumber_byflag</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">str_flag</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get number by flag</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flag2number</span><span class="p">[</span><span class="n">str_flag</span><span class="p">]</span>


<div class="viewcode-block" id="CNetMsg"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg">[docs]</a><span class="k">class</span> <span class="nc">CNetMsg</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    CNetMsg</span>
<span class="sd">        flag: System use only.</span>
<span class="sd">        type: System will use type &gt; 65535. Users will use type &lt;=65535</span>

<span class="sd">        #head  CUP012-3 for building connection</span>
<span class="sd">        #len - uint64</span>
<span class="sd">        #fromip,port, stub -uint64</span>
<span class="sd">        #toip,port, stub   -uint64</span>
<span class="sd">        #msg_type          -uint32</span>
<span class="sd">        #uniqid            -128bit [64bit ip, port, 64 bit, uniqid]</span>
<span class="sd">        #body           -no limit (length:uint64)</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># length 8</span>
    <span class="n">MSG_SIGN</span> <span class="o">=</span> <span class="s1">&#39;CUP012-3&#39;</span>

    <span class="n">_ORDER</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s1">&#39;head&#39;</span><span class="p">,</span> <span class="s1">&#39;flag&#39;</span><span class="p">,</span> <span class="s1">&#39;len&#39;</span><span class="p">,</span> <span class="s1">&#39;from&#39;</span><span class="p">,</span> <span class="s1">&#39;to&#39;</span><span class="p">,</span> <span class="s1">&#39;type&#39;</span><span class="p">,</span> <span class="s1">&#39;uniq_id&#39;</span><span class="p">,</span> <span class="s1">&#39;body&#39;</span>
    <span class="p">]</span>

    <span class="n">_SIZE_EXCEPT_BODY</span> <span class="o">=</span> <span class="mi">72</span>
    <span class="n">_SIZE_EXCEPT_HEAD_BODY</span> <span class="o">=</span> <span class="mi">64</span>
    <span class="n">_ORDER_COUNTS</span> <span class="o">=</span> <span class="mi">8</span>

    <span class="c1"># Default flags</span>
    <span class="n">MSG_FLAG_MAN</span> <span class="o">=</span> <span class="n">CMsgFlag</span><span class="p">()</span>
    <span class="n">MSG_FLAG_MAN</span><span class="o">.</span><span class="n">register_flags</span><span class="p">(</span><span class="n">MSG_FLAG2NUM</span><span class="p">)</span>

    <span class="c1"># Default types.</span>
    <span class="n">MSGTYPE</span> <span class="o">=</span> <span class="n">CMsgType</span><span class="p">()</span>
    <span class="n">_SYS_MSG_TYPES</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;ACK&#39;</span><span class="p">:</span> <span class="mi">65536</span>
    <span class="p">}</span>
    <span class="n">MSGTYPE</span><span class="o">.</span><span class="n">register_types</span><span class="p">(</span><span class="n">_SYS_MSG_TYPES</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">is_postmsg</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="c1">#super(self.__class__, self).__init__()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_ORDER_BYTES</span> <span class="o">=</span> <span class="p">[</span><span class="mi">8</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_is_postmsg</span> <span class="o">=</span> <span class="n">is_postmsg</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_need_head</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_read_order</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_writeindex</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_msg_finish</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_context</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_msglen</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_bodylen</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_type</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_uniqid</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fromaddr</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_toaddr</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_dumpdata</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_flag</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="c1"># self._del_timeout = None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_resend_flag</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_resend_times</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">if</span> <span class="n">is_postmsg</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">set_flag</span><span class="p">(</span>
                <span class="n">MSG_FLAG2NUM</span><span class="p">[</span><span class="s1">&#39;FLAG_NORMAL&#39;</span><span class="p">]</span>
            <span class="p">)</span>

        <span class="c1"># for CNeedAckMsg</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_errmsg</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_retry_interval</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_total_timeout</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_last_retry_time</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_callback_func</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_resend_flag</span> <span class="o">=</span> <span class="n">MSG_RESENDING_FLAG</span>


    <span class="k">def</span> <span class="nf">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;del the msg&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="s1">&#39;body&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">:</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;body&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dumpdata</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dumpdata</span>

<div class="viewcode-block" id="CNetMsg.get_order_counts"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_order_counts">[docs]</a>    <span class="k">def</span> <span class="nf">get_order_counts</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get order counts</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ORDER_COUNTS</span></div>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_asign_uint2byte_bybits</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">num</span><span class="p">,</span> <span class="n">bits</span><span class="p">):</span>
        <span class="n">asign_len</span> <span class="o">=</span> <span class="n">bits</span> <span class="o">/</span> <span class="mi">8</span>
        <span class="n">tmp</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">quotient</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">num</span> <span class="o">/</span> <span class="mi">256</span><span class="p">)</span>
            <span class="n">remainder</span> <span class="o">=</span> <span class="n">num</span> <span class="o">%</span> <span class="mi">256</span>
            <span class="n">tmp</span> <span class="o">+=</span> <span class="nb">chr</span><span class="p">(</span><span class="n">remainder</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">quotient</span> <span class="o">&lt;</span> <span class="mi">256</span><span class="p">:</span>
                <span class="n">tmp</span> <span class="o">+=</span> <span class="nb">chr</span><span class="p">(</span><span class="n">quotient</span><span class="p">)</span>
                <span class="k">break</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">num</span> <span class="o">=</span> <span class="n">quotient</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">length</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">tmp</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">length</span> <span class="o">&lt;</span> <span class="n">asign_len</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="n">xrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">asign_len</span> <span class="o">-</span> <span class="n">length</span><span class="p">):</span>
                <span class="n">tmp</span> <span class="o">+=</span> <span class="nb">chr</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">tmp</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_convert_bytes2uint</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">str_data</span><span class="p">):</span>
        <span class="n">num</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">b_ind</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">str_data</span><span class="p">:</span>
            <span class="n">num</span> <span class="o">+=</span> <span class="nb">pow</span><span class="p">(</span><span class="mi">256</span><span class="p">,</span> <span class="n">b_ind</span><span class="p">)</span> <span class="o">*</span> <span class="nb">ord</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
            <span class="n">b_ind</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="n">num</span>

<div class="viewcode-block" id="CNetMsg.push_data"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.push_data">[docs]</a>    <span class="k">def</span> <span class="nf">push_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        push data into the msg. Return pushed length.</span>

<span class="sd">        Return -1 if we should shutdown the socket channel.</span>

<span class="sd">        :raise exception:</span>
<span class="sd">            may raise IndexError when coming msg has problems.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_msg_finish</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;The CNetMsg has already been pushed enough data&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="mi">0</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                <span class="s1">&#39;You just pushed into the msg with a zero-length data&#39;</span>
            <span class="p">)</span>
            <span class="k">return</span> <span class="mi">0</span>
        <span class="n">sign</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">data_ind</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">data_max</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
        <span class="c1"># log.info(&#39;msg data read-order:{0}, context:{1}&#39;.format(self._read_order,</span>
        <span class="c1">#     self.get_msg_context().get_context_info()))</span>
        <span class="n">data_key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ORDER</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_read_order</span><span class="p">]</span>
        <span class="k">while</span> <span class="n">sign</span><span class="p">:</span>
            <span class="c1"># One loop handle one data_key until there all the data is handled.</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">data_key</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">data_key</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
            <span class="n">loop_data_max</span> <span class="o">=</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_ORDER_BYTES</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_read_order</span><span class="p">]</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">data_key</span><span class="p">])</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">data_max</span> <span class="o">-</span> <span class="n">data_ind</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="n">loop_data_max</span><span class="p">:</span>
                <span class="c1"># can fill up the msg</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">data_key</span><span class="p">]</span> <span class="o">+=</span> <span class="p">(</span>
                    <span class="n">data</span><span class="p">[</span><span class="n">data_ind</span><span class="p">:</span> <span class="n">loop_data_max</span> <span class="o">+</span> <span class="n">data_ind</span><span class="p">]</span>
                <span class="p">)</span>
                <span class="n">data_ind</span> <span class="o">+=</span> <span class="n">loop_data_max</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_read_order</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="n">data_key_info</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
                <span class="k">if</span> <span class="n">data_key</span> <span class="o">==</span> <span class="s1">&#39;head&#39;</span><span class="p">:</span>
                    <span class="n">data_key_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">data_key</span><span class="p">]</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">data_key</span><span class="p">]</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">MSG_SIGN</span><span class="p">:</span>
                        <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
                <span class="k">elif</span> <span class="n">data_key</span> <span class="o">==</span> <span class="s1">&#39;flag&#39;</span><span class="p">:</span>
                    <span class="n">data_key_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_flag</span><span class="p">()</span>
                <span class="k">elif</span> <span class="n">data_key</span> <span class="o">==</span> <span class="s1">&#39;len&#39;</span><span class="p">:</span>
                    <span class="n">total_len</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_msg_len</span><span class="p">()</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_need_head</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_ORDER_BYTES</span><span class="p">[</span><span class="mi">7</span><span class="p">]</span> <span class="o">=</span> <span class="n">total_len</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_SIZE_EXCEPT_BODY</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_ORDER_BYTES</span><span class="p">[</span><span class="mi">7</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span>
                            <span class="n">total_len</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_SIZE_EXCEPT_HEAD_BODY</span>
                        <span class="p">)</span>
                    <span class="n">data_key_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_msg_len</span><span class="p">()</span>
                <span class="k">elif</span> <span class="n">data_key</span> <span class="o">==</span> <span class="s1">&#39;from&#39;</span><span class="p">:</span>
                    <span class="n">data_key_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_from_addr</span><span class="p">()</span>
                <span class="k">elif</span> <span class="n">data_key</span> <span class="o">==</span> <span class="s1">&#39;uniq_id&#39;</span><span class="p">:</span>
                    <span class="n">data_key_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_uniq_id</span><span class="p">()</span>
                <span class="k">elif</span> <span class="n">data_key</span> <span class="o">==</span> <span class="s1">&#39;type&#39;</span><span class="p">:</span>
                    <span class="n">data_key_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_msg_type</span><span class="p">()</span>
                <span class="k">elif</span> <span class="n">data_key</span> <span class="o">==</span> <span class="s1">&#39;body&#39;</span><span class="p">:</span>
                    <span class="n">data_key_info</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;body&#39;</span><span class="p">])</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_read_order</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ORDER_COUNTS</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_msg_finish</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="n">sign</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                        <span class="s1">&#39;congratulations. &#39;</span>
                        <span class="s1">&#39;This msg(</span><span class="si">{0}</span><span class="s1"> </span><span class="si">{1}</span><span class="s1">) has been filled&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">get_uniq_id</span><span class="p">(),</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">get_msg_context</span><span class="p">()</span><span class="o">.</span><span class="n">get_context_info</span><span class="p">())</span>
                    <span class="p">)</span>
                    <span class="k">break</span>
                <span class="n">data_key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ORDER</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_read_order</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># cannot fill up the msg in this round</span>
                <span class="n">sign</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="n">push_bytes</span> <span class="o">=</span> <span class="n">data_max</span> <span class="o">-</span> <span class="n">data_ind</span>
                <span class="c1"># self._data[data_key] += data[data_ind: data_max]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">data_key</span><span class="p">]</span> <span class="o">+=</span> <span class="n">data</span><span class="p">[</span><span class="n">data_ind</span><span class="p">:]</span>
                <span class="n">data_ind</span> <span class="o">+=</span> <span class="n">push_bytes</span>
        <span class="k">return</span> <span class="n">data_ind</span></div>

    <span class="k">def</span> <span class="nf">_addr2pack</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ip_port</span><span class="p">,</span> <span class="n">stub_future</span><span class="p">):</span>
        <span class="n">misc</span><span class="o">.</span><span class="n">check_type</span><span class="p">(</span><span class="n">ip_port</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)</span>
        <span class="n">misc</span><span class="o">.</span><span class="n">check_type</span><span class="p">(</span><span class="n">stub_future</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)</span>
        <span class="n">pack</span> <span class="o">=</span> <span class="n">common</span><span class="o">.</span><span class="n">ip_port2connaddr</span><span class="p">(</span><span class="n">ip_port</span><span class="p">)</span>
        <span class="n">pack</span> <span class="o">=</span> <span class="n">common</span><span class="o">.</span><span class="n">add_stub2connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">,</span> <span class="n">stub_future</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">pack</span> <span class="o">=</span> <span class="n">common</span><span class="o">.</span><span class="n">add_future2connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">,</span> <span class="n">stub_future</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">pack</span>

<div class="viewcode-block" id="CNetMsg.set_flag"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_flag">[docs]</a>    <span class="k">def</span> <span class="nf">set_flag</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flag</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set flag for the msg</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># misc.check_type(flag, int)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_flag</span> <span class="o">=</span> <span class="n">flag</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;flag&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_asign_uint2byte_bybits</span><span class="p">(</span><span class="n">flag</span><span class="p">,</span> <span class="mi">32</span><span class="p">)</span></div>

<div class="viewcode-block" id="CNetMsg.set_resend_flag"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_resend_flag">[docs]</a>    <span class="k">def</span> <span class="nf">set_resend_flag</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">handle_flag</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set msg handle flag</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_resend_flag</span> <span class="o">=</span> <span class="n">handle_flag</span></div>

<div class="viewcode-block" id="CNetMsg.get_resend_flag"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_resend_flag">[docs]</a>    <span class="k">def</span> <span class="nf">get_resend_flag</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get msg handle flag</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_resend_flag</span></div>

<div class="viewcode-block" id="CNetMsg.add_flag"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.add_flag">[docs]</a>    <span class="k">def</span> <span class="nf">add_flag</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flag</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;add flag into the msg&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_flag</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flag</span> <span class="o">|</span> <span class="n">flag</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;flag&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_asign_uint2byte_bybits</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_flag</span><span class="p">,</span> <span class="mi">32</span><span class="p">)</span></div>

<div class="viewcode-block" id="CNetMsg.set_need_head"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_need_head">[docs]</a>    <span class="k">def</span> <span class="nf">set_need_head</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">b_need</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :note:</span>
<span class="sd">            By default, the msg does not need to have a head unless</span>
<span class="sd">            it&#39;s the first msg that posted/received.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_need_head</span> <span class="o">=</span> <span class="n">b_need</span>
        <span class="k">if</span> <span class="n">b_need</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_read_order</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_read_order</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_postmsg</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_need_head</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;head&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">MSG_SIGN</span></div>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_addr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">ip_port</span><span class="p">,</span> <span class="n">stub_future</span><span class="p">):</span>
        <span class="n">ip</span><span class="p">,</span> <span class="n">port</span> <span class="o">=</span> <span class="n">ip_port</span>
        <span class="n">stub</span><span class="p">,</span> <span class="n">future</span> <span class="o">=</span> <span class="n">stub_future</span>
        <span class="n">misc</span><span class="o">.</span><span class="n">check_type</span><span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_set_msg_len</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_need_head</span><span class="p">:</span>
            <span class="n">size_except_body</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_SIZE_EXCEPT_BODY</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">size_except_body</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_SIZE_EXCEPT_HEAD_BODY</span>
        <span class="n">body_len</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;body&#39;</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_ORDER_BYTES</span><span class="p">[</span><span class="mi">7</span><span class="p">]</span> <span class="o">=</span> <span class="n">body_len</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_msglen</span> <span class="o">=</span> <span class="n">body_len</span> <span class="o">+</span> <span class="n">size_except_body</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;len&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_asign_uint2byte_bybits</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_msglen</span><span class="p">,</span> <span class="mi">64</span>
        <span class="p">)</span>
        <span class="n">tempstr</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">xrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ORDER_COUNTS</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_need_head</span><span class="p">):</span>
                <span class="k">continue</span>
            <span class="n">tempstr</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_ORDER</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_dumpdata</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{0}{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">tempstr</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;body&#39;</span><span class="p">])</span>

<div class="viewcode-block" id="CNetMsg.set_from_addr"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_from_addr">[docs]</a>    <span class="k">def</span> <span class="nf">set_from_addr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ip_port</span><span class="p">,</span> <span class="n">stub_future</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set msg from addr</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_check_addr</span><span class="p">(</span><span class="n">ip_port</span><span class="p">,</span> <span class="n">stub_future</span><span class="p">)</span>
        <span class="n">pack</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_addr2pack</span><span class="p">(</span><span class="n">ip_port</span><span class="p">,</span> <span class="n">stub_future</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;from&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_asign_uint2byte_bybits</span><span class="p">(</span><span class="n">pack</span><span class="p">,</span> <span class="mi">128</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fromaddr</span> <span class="o">=</span> <span class="p">(</span><span class="n">ip_port</span><span class="p">,</span> <span class="n">stub_future</span><span class="p">)</span></div>

<div class="viewcode-block" id="CNetMsg.set_to_addr"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_to_addr">[docs]</a>    <span class="k">def</span> <span class="nf">set_to_addr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ip_port</span><span class="p">,</span> <span class="n">stub_future</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set msg to addr</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_check_addr</span><span class="p">(</span><span class="n">ip_port</span><span class="p">,</span> <span class="n">stub_future</span><span class="p">)</span>
        <span class="n">pack</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_addr2pack</span><span class="p">(</span><span class="n">ip_port</span><span class="p">,</span> <span class="n">stub_future</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_asign_uint2byte_bybits</span><span class="p">(</span><span class="n">pack</span><span class="p">,</span> <span class="mi">128</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_toaddr</span> <span class="o">=</span> <span class="p">(</span><span class="n">ip_port</span><span class="p">,</span> <span class="n">stub_future</span><span class="p">)</span></div>

<div class="viewcode-block" id="CNetMsg.set_msg_type"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_msg_type">[docs]</a>    <span class="k">def</span> <span class="nf">set_msg_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg_type</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set msg type</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">misc</span><span class="o">.</span><span class="n">check_type</span><span class="p">(</span><span class="n">msg_type</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_asign_uint2byte_bybits</span><span class="p">(</span><span class="n">msg_type</span><span class="p">,</span> <span class="mi">32</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_type</span> <span class="o">=</span> <span class="n">msg_type</span></div>

<div class="viewcode-block" id="CNetMsg.set_uniq_id"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_uniq_id">[docs]</a>    <span class="k">def</span> <span class="nf">set_uniq_id</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">uniq_id</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set msg unique id</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># misc.check_type(uniq_id, int)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;uniq_id&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_asign_uint2byte_bybits</span><span class="p">(</span><span class="n">uniq_id</span><span class="p">,</span> <span class="mi">128</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_uniqid</span> <span class="o">=</span> <span class="n">uniq_id</span></div>

<div class="viewcode-block" id="CNetMsg.set_body"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_body">[docs]</a>    <span class="k">def</span> <span class="nf">set_body</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">body</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set msg body</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">misc</span><span class="o">.</span><span class="n">check_type</span><span class="p">(</span><span class="n">body</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;body&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">body</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_bodylen</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">body</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_pack_toaddr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pack</span><span class="p">):</span>
        <span class="p">(</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">)</span> <span class="o">=</span> <span class="n">common</span><span class="o">.</span><span class="n">get_ip_and_port_connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">)</span>
        <span class="n">stub</span> <span class="o">=</span> <span class="n">common</span><span class="o">.</span><span class="n">getstub_connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">)</span>
        <span class="n">future</span> <span class="o">=</span> <span class="n">common</span><span class="o">.</span><span class="n">getfuture_connaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">((</span><span class="n">ip</span><span class="p">,</span> <span class="n">port</span><span class="p">),</span> <span class="p">(</span><span class="n">stub</span><span class="p">,</span> <span class="n">future</span><span class="p">))</span>

<div class="viewcode-block" id="CNetMsg.get_flag"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_flag">[docs]</a>    <span class="k">def</span> <span class="nf">get_flag</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get msg flag</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flag</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_flag</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convert_bytes2uint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;flag&#39;</span><span class="p">])</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_flag</span></div>

<div class="viewcode-block" id="CNetMsg.get_to_addr"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_to_addr">[docs]</a>    <span class="k">def</span> <span class="nf">get_to_addr</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get to addr</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_toaddr</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">pack</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convert_bytes2uint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_toaddr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pack_toaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_toaddr</span></div>

<div class="viewcode-block" id="CNetMsg.get_from_addr"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_from_addr">[docs]</a>    <span class="k">def</span> <span class="nf">get_from_addr</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get from addr. ((ip, port), (stub, future))</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fromaddr</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">pack</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convert_bytes2uint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;from&#39;</span><span class="p">])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_fromaddr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_pack_toaddr</span><span class="p">(</span><span class="n">pack</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fromaddr</span></div>

<div class="viewcode-block" id="CNetMsg.get_msg_type"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_msg_type">[docs]</a>    <span class="k">def</span> <span class="nf">get_msg_type</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get msg type</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_type</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_type</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convert_bytes2uint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">])</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_type</span></div>

<div class="viewcode-block" id="CNetMsg.get_msg_len"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_msg_len">[docs]</a>    <span class="k">def</span> <span class="nf">get_msg_len</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get msg len</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_msglen</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_msglen</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convert_bytes2uint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;len&#39;</span><span class="p">])</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_msglen</span></div>

<div class="viewcode-block" id="CNetMsg.get_uniq_id"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_uniq_id">[docs]</a>    <span class="k">def</span> <span class="nf">get_uniq_id</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get unique msg id</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_uniqid</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_uniqid</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convert_bytes2uint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;uniq_id&#39;</span><span class="p">])</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_uniqid</span></div>

<div class="viewcode-block" id="CNetMsg.get_body"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_body">[docs]</a>    <span class="k">def</span> <span class="nf">get_body</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get msg body</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="s1">&#39;body&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s1">&#39;Body not set yet&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="s1">&#39;body&#39;</span><span class="p">]</span></div>

<div class="viewcode-block" id="CNetMsg.get_bodylen"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_bodylen">[docs]</a>    <span class="k">def</span> <span class="nf">get_bodylen</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get body length</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bodylen</span></div>

<div class="viewcode-block" id="CNetMsg.is_a_sendmsg"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.is_a_sendmsg">[docs]</a>    <span class="k">def</span> <span class="nf">is_a_sendmsg</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        is a msg being sent</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_postmsg</span></div>

<div class="viewcode-block" id="CNetMsg.is_a_recvmsg"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.is_a_recvmsg">[docs]</a>    <span class="k">def</span> <span class="nf">is_a_recvmsg</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        is a msg being received</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_postmsg</span><span class="p">)</span></div>

<div class="viewcode-block" id="CNetMsg.is_recvmsg_complete"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.is_recvmsg_complete">[docs]</a>    <span class="k">def</span> <span class="nf">is_recvmsg_complete</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        is msg received already</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_postmsg</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_msg_finish</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span></div>

    <span class="c1"># the head in self._data should be set before sent.</span>
    <span class="c1"># Thus, the self._data.keys will be 1 less than</span>
    <span class="c1"># self.get_order_counts()</span>
<div class="viewcode-block" id="CNetMsg.is_sendmsg_complete"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.is_sendmsg_complete">[docs]</a>    <span class="k">def</span> <span class="nf">is_sendmsg_complete</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        is msg sent complete</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_need_head</span><span class="p">:</span>
            <span class="n">size_except_body</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_SIZE_EXCEPT_BODY</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">size_except_body</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_SIZE_EXCEPT_HEAD_BODY</span>
        <span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_bodylen</span> <span class="o">+</span> <span class="n">size_except_body</span><span class="p">)</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_msglen</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span></div>

<div class="viewcode-block" id="CNetMsg.get_write_bytes"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_write_bytes">[docs]</a>    <span class="k">def</span> <span class="nf">get_write_bytes</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">length</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get write bytes from the msg</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">length</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="c1"># log.debug(</span>
        <span class="c1">#     &#39;to get {0} write bytes from msg, &#39;</span>
        <span class="c1">#     &#39;_writeindex:{1}, msg total_len: {2}&#39;.format(</span>
        <span class="c1">#         length, self._writeindex, len(self._dumpdata)</span>
        <span class="c1">#     )</span>
        <span class="c1"># )</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dumpdata</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_writeindex</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_writeindex</span> <span class="o">+</span> <span class="n">length</span><span class="p">]</span></div>

<div class="viewcode-block" id="CNetMsg.seek_write"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.seek_write">[docs]</a>    <span class="k">def</span> <span class="nf">seek_write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">length_ahead</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        seek foreward by length</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># log.debug(</span>
        <span class="c1">#     &#39;to seek msg length {0}, now index {1}&#39;.format(</span>
        <span class="c1">#         length_ahead, self._writeindex))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_writeindex</span> <span class="o">+=</span> <span class="n">length_ahead</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_writeindex</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_msg_len</span><span class="p">():</span>
            <span class="k">raise</span> <span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">AsyncMsgError</span><span class="p">(</span>
                <span class="s1">&#39;You have seek_write out of the msg length&#39;</span>
            <span class="p">)</span></div>

<div class="viewcode-block" id="CNetMsg.is_msg_already_sent"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.is_msg_already_sent">[docs]</a>    <span class="k">def</span> <span class="nf">is_msg_already_sent</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        is msg already sent</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_writeindex</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_msg_len</span><span class="p">():</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span></div>

    <span class="c1">#this function is only used by msg which need to be ack</span>
    <span class="c1">#need ack msg</span>
<div class="viewcode-block" id="CNetMsg.pre_resend"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.pre_resend">[docs]</a>    <span class="k">def</span> <span class="nf">pre_resend</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set writeindex</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_writeindex</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_msg_finish</span> <span class="o">=</span> <span class="kc">False</span></div>

<div class="viewcode-block" id="CNetMsg.set_errmsg"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_errmsg">[docs]</a>    <span class="k">def</span> <span class="nf">set_errmsg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">errmsg</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;set errmsg when we encounter errors sending it out&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_errmsg</span> <span class="o">=</span> <span class="n">errmsg</span></div>

<div class="viewcode-block" id="CNetMsg.get_errmsg"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_errmsg">[docs]</a>    <span class="k">def</span> <span class="nf">get_errmsg</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;get errmsg if we encounter errors sending it out&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_errmsg</span></div>

<div class="viewcode-block" id="CNetMsg.set_total_timeout"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_total_timeout">[docs]</a>    <span class="k">def</span> <span class="nf">set_total_timeout</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">total_timeout</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set total_timeout</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_total_timeout</span> <span class="o">=</span> <span class="n">total_timeout</span></div>

<div class="viewcode-block" id="CNetMsg.set_retry_interval"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_retry_interval">[docs]</a>    <span class="k">def</span> <span class="nf">set_retry_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">retry_interval</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set retry_interval</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_retry_interval</span> <span class="o">=</span> <span class="n">retry_interval</span></div>

<div class="viewcode-block" id="CNetMsg.set_callback_function"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_callback_function">[docs]</a>    <span class="k">def</span> <span class="nf">set_callback_function</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">function</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set function</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_callback_func</span> <span class="o">=</span> <span class="n">function</span></div>

<div class="viewcode-block" id="CNetMsg.set_last_retry_time"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_last_retry_time">[docs]</a>    <span class="k">def</span> <span class="nf">set_last_retry_time</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">last_retry_time</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set last_retry_time</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_last_retry_time</span> <span class="o">=</span> <span class="n">last_retry_time</span></div>

<div class="viewcode-block" id="CNetMsg.get_total_timeout"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_total_timeout">[docs]</a>    <span class="k">def</span> <span class="nf">get_total_timeout</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get total_timeout</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_total_timeout</span></div>

<div class="viewcode-block" id="CNetMsg.get_retry_interval"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_retry_interval">[docs]</a>    <span class="k">def</span> <span class="nf">get_retry_interval</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get retry_interval</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_retry_interval</span></div>

<div class="viewcode-block" id="CNetMsg.get_callback_function"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_callback_function">[docs]</a>    <span class="k">def</span> <span class="nf">get_callback_function</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get callback function</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_callback_func</span></div>

<div class="viewcode-block" id="CNetMsg.get_last_retry_time"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_last_retry_time">[docs]</a>    <span class="k">def</span> <span class="nf">get_last_retry_time</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get last_retry_time</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_last_retry_time</span></div>

<div class="viewcode-block" id="CNetMsg.set_retry_times"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_retry_times">[docs]</a>    <span class="k">def</span> <span class="nf">set_retry_times</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;set msg retry times&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_resend_times</span> <span class="o">=</span> <span class="n">num</span></div>

<div class="viewcode-block" id="CNetMsg.add_retry_times"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.add_retry_times">[docs]</a>    <span class="k">def</span> <span class="nf">add_retry_times</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;add retry times&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_resend_times</span> <span class="o">+=</span> <span class="mi">1</span></div>

<div class="viewcode-block" id="CNetMsg.get_retry_times"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_retry_times">[docs]</a>    <span class="k">def</span> <span class="nf">get_retry_times</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;get retry times&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_resend_times</span></div>

<div class="viewcode-block" id="CNetMsg.set_msg_context"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.set_msg_context">[docs]</a>    <span class="k">def</span> <span class="nf">set_msg_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set up context for this netmsg</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_context</span> <span class="o">=</span> <span class="n">context</span></div>

<div class="viewcode-block" id="CNetMsg.get_msg_context"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.get_msg_context">[docs]</a>    <span class="k">def</span> <span class="nf">get_msg_context</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get msg context</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_context</span></div>

<div class="viewcode-block" id="CNetMsg.is_valid4send"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CNetMsg.is_valid4send">[docs]</a>    <span class="k">def</span> <span class="nf">is_valid4send</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">netmsg</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        for future use</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="kc">True</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span></div></div>


<span class="c1"># pylint: disable=R0904</span>
<span class="k">class</span> <span class="nc">CNeedAckMsg</span><span class="p">(</span><span class="n">CNetMsg</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Class need ack msg</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">retry_interval</span><span class="p">,</span> <span class="n">total_timeout</span><span class="p">,</span> <span class="n">function</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param function:</span>
<span class="sd">            Whether succeed or not, the framework will invoke the function</span>
<span class="sd">            passed in.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">CNetMsg</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">is_postmsg</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_flag</span><span class="p">(</span><span class="n">MSG_FLAG2NUM</span><span class="p">[</span><span class="s1">&#39;FLAG_NEEDACK&#39;</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_retry_interval</span> <span class="o">=</span> <span class="n">retry_interval</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_total_timeout</span> <span class="o">=</span> <span class="n">total_timeout</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_last_retry_time</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_callback_func</span> <span class="o">=</span> <span class="n">function</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_resend_flag</span> <span class="o">=</span> <span class="n">MSG_RESENDING_FLAG</span>


<span class="c1"># pylint: disable=R0904</span>
<div class="viewcode-block" id="CAckMsg"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.CAckMsg">[docs]</a><span class="k">class</span> <span class="nc">CAckMsg</span><span class="p">(</span><span class="n">CNetMsg</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    ack msg example</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">is_postmsg</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="n">CNetMsg</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">is_postmsg</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_flag</span><span class="p">(</span><span class="n">MSG_FLAG2NUM</span><span class="p">[</span><span class="s1">&#39;FLAG_ACK&#39;</span><span class="p">])</span></div>


<div class="viewcode-block" id="netmsg_tostring"><a class="viewcode-back" href="../../../../../cup.net.async/#cup.net.async.msg.netmsg_tostring">[docs]</a><span class="k">def</span> <span class="nf">netmsg_tostring</span><span class="p">(</span><span class="n">netmsg</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get printable netmsg</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">msg</span> <span class="o">=</span> <span class="p">(</span>
        <span class="s1">&#39;netmsg, from </span><span class="si">{0}</span><span class="s1"> to </span><span class="si">{1}</span><span class="s1">, uniqid </span><span class="si">{2}</span><span class="s1">, msg_type </span><span class="si">{3}</span><span class="s1">, flag </span><span class="si">{4}</span><span class="s1">, &#39;</span>
        <span class="s1">&#39;body_len </span><span class="si">{5}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="nb">str</span><span class="p">(</span><span class="n">netmsg</span><span class="o">.</span><span class="n">get_from_addr</span><span class="p">()),</span> <span class="nb">str</span><span class="p">(</span><span class="n">netmsg</span><span class="o">.</span><span class="n">get_to_addr</span><span class="p">()),</span>
            <span class="n">netmsg</span><span class="o">.</span><span class="n">get_uniq_id</span><span class="p">(),</span> <span class="n">netmsg</span><span class="o">.</span><span class="n">get_msg_type</span><span class="p">(),</span>
            <span class="n">netmsg</span><span class="o">.</span><span class="n">get_flag</span><span class="p">(),</span> <span class="n">netmsg</span><span class="o">.</span><span class="n">get_bodylen</span><span class="p">()</span>
        <span class="p">)</span>
    <span class="p">)</span>
    <span class="k">return</span> <span class="n">msg</span></div>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">gen</span> <span class="o">=</span> <span class="n">generator</span><span class="o">.</span><span class="n">CycleIDGenerator</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="s1">&#39;5000&#39;</span><span class="p">)</span>
    <span class="n">gen_id</span> <span class="o">=</span> <span class="n">gen</span><span class="o">.</span><span class="n">next_id</span><span class="p">()</span>
    <span class="n">str_num</span> <span class="o">=</span>  <span class="n">CNetMsg</span><span class="o">.</span><span class="n">_asign_uint2byte_bybits</span><span class="p">(</span><span class="n">gen_id</span><span class="p">,</span> <span class="mi">128</span><span class="p">)</span>
    <span class="nb">print</span> <span class="n">gen_id</span>
    <span class="nb">print</span> <span class="nb">len</span><span class="p">(</span><span class="n">str_num</span><span class="p">)</span>


<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../../../',
            VERSION:'1.7.0',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  <script type="text/javascript" src="../../../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>